﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MP3Ganizer.Common.BaseObject;
using MP3Ganizer.Common.Interfaces.FileTree;

namespace MP3Ganizer.Modules.FileTree.ViewModels
{
    public class FileItemViewModel : BaseModuleViewModel, IFileItemViewModel
    {
        private BusinessObject.Infos.MP3Infos _info;
        private int _status;
        public void Initialize(BusinessObject.Infos.MP3Infos info)
        {
            _info = info;
            SetStatus();
        }

        public IEnumerable<string> Artists
        {
            get { return _info.Artists.ToArray(); }
            set 
            { 
                _info.Artists = value;
                SetStatus();
                OnPropertyChanged("Artists");
            }
        }
        public string ArtistsSortMember
        {
            get { return string.Join(",", _info.Artists.OrderBy(item => item)); }
        }
        public string Album 
        {
            get { return _info.Album; }
            set 
            { 
               _info.Album = value;
                SetStatus();
                OnPropertyChanged("Album");
            }
        }

        public IEnumerable<string> Images
        {
            get { return _info.Images; }
        }

        public string Title
        {
            get { return  _info.Title; }
            set 
            { 
                _info.Title = value;
                SetStatus();
                OnPropertyChanged("Title");
            }
        }

        public int TrackNumber
        {
            get { return _info.TrackNumber; }
            set
            {
                _info.TrackNumber = value;
                SetStatus();
                OnPropertyChanged("TrackNumber");
            }
        }

        public string SourcePath
        {
            get { return _info.SourcePath; }
            set
            {
                _info.SourcePath = value;
                OnPropertyChanged("SourcePath");
            }
        }


        public int Status
        {
            get { return _status; }
        }

        private void SetStatus()
        {
            _status = 0;
            if (!string.IsNullOrEmpty(_info.Title))
                _status += 35;
            if (_info.Artists != null && _info.Artists.FirstOrDefault(item => !string.IsNullOrEmpty(item)) != null)
                _status += 30;
            if (!string.IsNullOrEmpty(_info.Album))
                _status += 20;
            if (_info.TrackNumber != 0)
                _status += 15;
            OnPropertyChanged("Status");
        }

    }
}
